package com.zcwin.runner.model;

import java.util.Vector;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

import com.zcwin.runner.database.DAOFactory;
import com.zcwin.runner.database.IDAO;
import com.zcwin.runner.sms.bean.SendedSmsBean;
import com.zcwin.runner.sms.bean.SmsConfig;
import com.zcwin.runner.sms.bean.SmsServerConfig;
import com.zcwin.runner.utils.GTime;
import com.zcwin.runner.utils.GTimeExt;


/**
 *
 *
 * @author 王士江
 * @version 1.0
 */
public class QuerySmsUtil {
    IDAO dao = DAOFactory.getDao("0");
    public Vector querySendedData()
    {

    	String sTime = GTime.getTime(GTime.YYYYMMDDhhmmss);
    	String sSql = "SELECT ID,RECVMOBILE,SMSSTATE,PTIME,PCOUNT,RETRY,SMSID FROM SMS_LOG  WHERE SMSSTATE = '1' AND PCOUNT < RETRY "
    			+ "AND PTIME<'"+sTime+"' ORDER BY PCOUNT ASC,PRIO DESC,PTIME ASC ";
        Vector ve = dao.selectEx(sSql, 10);
        if(ve!=null && ve.size()>0)
        {
           return ve;
        }
        return null;
    }
    
    public String querySms(SendedSmsBean sb)
    {
        String ret = "ERROR";
        Object[] objects;
		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
		// 创建wsdl访问客户端
		Client client = dcf.createClient(sb.getUrl());		
		try 
		{
			//测试Query方法
			String query = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
					+"<Root><QueryInfo><Msg>"
					+"<Id>"+sb.getSmsid()+"</Id>"
					+"<PhoneNO>"+sb.getDestAddr()+"</PhoneNO>"
					+"</Msg></QueryInfo></Root>";
			objects = client.invoke("Query", query);
			ret = objects[0].toString();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
        return ret;
    }
    public void setQuerySucess(String smsId){
        String sTime = GTime.getTime(GTime.YYYYMMDDhhmmss);
        String sql = "UPDATE SMS_LOG SET PTIME='"+sTime+"',SMSSTATE='6',PCOUNT=PCOUNT+1 WHERE ID='" + smsId + "'";
        dao.update(sql);
    }
    public void setQueryError(String smsId,int smscallback){
        String sTime = GTime.getTime(GTime.YYYYMMDDhhmmss);
        String tTime = GTimeExt.timeAccount(sTime, smscallback, 5, "yyyyMMddHHmmss");
        String sql = "UPDATE SMS_LOG SET PTIME='"+tTime+"',PCOUNT=PCOUNT+1 WHERE ID='" + smsId + "'";
        dao.update(sql);
    }
    public void setQueryError(String smsId){
    	String sTime = GTime.getTime(GTime.YYYYMMDDhhmmss);
    	String sql = "UPDATE SMS_LOG SET PTIME='"+sTime+"',SMSSTATE='7',PCOUNT=PCOUNT+1 WHERE ID='" + smsId + "'";
    	dao.update(sql);
    }
}

